package com.yj.jason.jsondemo.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.banzhi.emptylibrary.interfaces.OnLayoutClickListener;
import com.yj.jason.baselibrary.base.BaseFragment;
import com.yj.jason.baselibrary.view.RecyclerView.CommonAdapter.CommonRecyclerAdapter;
import com.yj.jason.baselibrary.view.RecyclerView.CommonAdapter.ViewHolder;
import com.yj.jason.jsondemo.Person;
import com.yj.jason.jsondemo.R;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("ValidFragment")
public class Fragment01 extends BaseFragment{

    private TextView tv;
    private RecyclerView cy;

    private String mContent;

    private List<Person> mBeans=new ArrayList<>();


    @SuppressLint("ValidFragment")
    public Fragment01(String content){
        this.mContent=content;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragmet01,container,false);

        tv=view.findViewById(R.id.tv);
        cy=view.findViewById(R.id.recyclerView);


        for(int i=0;i<20;i++){
            Person person=new Person("总页防水等级"+i,i);
            mBeans.add(person);
        }

        cy.setLayoutManager(new LinearLayoutManager(getActivity()));
        cy.setAdapter(new CommonRecyclerAdapter<Person>(getActivity(),mBeans,R.layout.item_table2) {
            @Override
            public void convert(ViewHolder holder, Person item) {
                holder.setText(R.id.tv_table,item.getName());
            }


        });

        return view;
    }

    @Override
    public View getLayoutView() {
        return cy;
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        super.onFragmentVisibleChange(isVisible);

        if(isVisible) {
            Log.i("tag","fragment01显示");


          //  tv.setText(mContent);


        }else {
            Log.i("tag","fragment01 隐藏");
        }
    }


    @Override
    protected void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();

        Log.i("tag","fragment01第一次加载");
        helper.init(new OnLayoutClickListener() {
            @Override
            public void onEmptyViewClick() {
                Toast.makeText(getActivity(),"重新加载",Toast.LENGTH_SHORT).show();
                helper.showContent();
            }

            @Override
            public void onErrorViewClick() {
                Toast.makeText(getActivity(),"重新加载",Toast.LENGTH_SHORT).show();
            }
        });

        helper.showLoading();


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                helper.showEmpty();
            }
        },3000);

    }
}

